const send = require('koa-send');
const admin = async (ctx, next) => {
  let done = false;
  if ((ctx.method === 'HEAD' || ctx.method === 'GET')) {
    let opts = {};
    console.log(ctx.path);
    if (ctx.path == '/admin' || ctx.path == '/login') {
     ctx.path = '/admin/dist/index.html'
    } else {
      opts = {
        root: __dirname + '/../admin/dist'
      }
    }
    try {
      done = await send(ctx, ctx.path, opts)
    } catch (err) {
      if (err.status !== 404) {
        throw err
      }
    }
  }
  if (!done) {
    await next();
  }
}
module.exports = admin;